package com.oa.tools.jobs.impl;

import java.util.List;
import java.util.Map;

import org.dom4j.Node;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.FlowLog;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.OrgElement;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.XmlUtil;
import com.oa.tools.utils.XmlUtil.XMLHandler;
import com.oa.tools.utils.http.EKPRequestClient;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

public class DelPersonTask extends FlowHandlerTaskBase{

	
	public DelPersonTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
		
		this.flowStatus = FlowHandlerTaskBase.FLOW_STATUS_DONE;//FLOW_STATUS_ING;
		this.templateId = "15f5736a36a96067c5845d24b21a20ee";
		
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		String xmlStr = obj.getExtendDataXml();
//		System.out.println(xmlStr);
		String fdId = obj.getFdId();
//		Example example_log = new Example(FlowLog.class);
//		Criteria c = example_log.createCriteria();
//		c.andEqualTo("fdProcessId", fdId);
//		Criteria c1 = example_log.createCriteria();
//		c1.orEqualTo("fdFactNodeId", "N79").orEqualTo("fdFactNodeId", "N81");
//		example_log.and(c1);
//		
//		List<FlowLog> flowLogs = this.flowLogDAO.selectByExample(example_log);
//		boolean hasAudit = false;
//		if(flowLogs==null || flowLogs.size()==0) {
//			return null;
//		}else {
//			for(FlowLog flowLog:flowLogs) {
//				if(flowLog.getFdStatus().equals(FlowHandlerTaskBase.FLOW_STATUS_DONE)) {
//					hasAudit= true;
//				}
//			}
//		}
//		if(!hasAudit) {
//			return null;
//		}
		
		String applyPersonId = "fd_35a11751608876";
		StringBuilder content = new StringBuilder();
		XmlUtil.parseXmlWithHandler(xmlStr, "/java/object/void",new XMLHandler() {
			@Override
			public void hanle(Node paramNode) throws Exception {
				Node ele1 = paramNode.selectSingleNode("string");
				if(applyPersonId.equals(ele1.getText())) {
					Node personIdNode= paramNode.selectSingleNode("object/void/string[2]");
					String personId = personIdNode.getText();
					OrgElement personEle = orgElementDAO.selectByPrimaryKey(personId);
					if(personEle.getFdIsAvailable()==1) {
						String result = EKPRequestClient.invalidatePeople2Ekp(personId);
						if(notice) {
							content.append(personEle.getFdId()+"-"+personEle.getFdName()+"\r\n"+result.replace("<", "&lt;\r\n").replace(">", "&gt;\r\n").replaceAll("\\s+", ""));
						}
						
					}
				}
			}
			
		});
		
		
		return content.toString();
	}

	
}
